bleh
[mqlkit.git] / SpeedTrain Zarko.mq4
blob7e59754c0edfed63a61de68407aeb4ada1079406
1 //+------------------------------------------------------------------+\r
2 //|                                             SpeedTrain Zarko.mq4 |\r
3 //|                      Copyright © 2011, MetaQuotes Software Corp. |\r
4 //|                                        http://www.metaquotes.net |\r
5 //+------------------------------------------------------------------+\r
6 #property copyright "Copyright © 2011, MetaQuotes Software Corp."\r
7 #property link      "http://www.metaquotes.net"\r
8 \r
9 \r
10 #include <stderror.mqh>\r
11 #include <stdlib.mqh>\r
15 extern int    Slippage         =       3;\r
20 extern double     LotSize= 0.1;\r
22 extern string PipStep_Remarks="------ Pip Distance. 30 pips is 30 pips for all brokers -------";\r
23 extern double     PipStep= 50;\r
25 extern string MoveSL2BE_Remarks="------ Move Prev order SL to BE ------";\r
26 extern bool  MoveSL2BE = true;\r
28 extern string InitialBuyOrder_Remarks="------ First Order on start, if true its LONG ------";\r
29 extern bool  InitialBuyOrder = true;\r
31 extern string UseTradingHours_Remarks="------ Place orders only during these hours. however closing can happen any time ------";\r
32 extern bool   UseTradingHours     =    false;\r
33 extern int    StartHour           =       7;\r
34 extern int    FinishHour          =      19;\r
36 extern string ProfitPct_Remarks="------ Profit Percentage ------";\r
37 extern double Profit_Fixed= 100.0;\r
38 extern double Panic_At = 80.0;\r
39 extern bool Paranoid_Stop_Loss = true;\r
41 extern string Bollinger_Bands_Remarks = "Difference between upper and lower Bollinger bounds must be above the value in order to place trades.";\r
42 extern bool Use_Bollinger_Bands = true;\r
43 extern double Min_Bollinger_Bands_Width = 0.02;\r
44 extern int Bollinger_Band_Days = 30;\r
45 extern int Bollinger_Band_Deviation = 3;\r
47 extern int    MagicNumber         =  1771611;\r
49 int longOrderCount=0;\r
50 int shortOrderCount=0;\r
51 double DollarProfitTarget;\r
52 double valLastFlatEq;\r
53 double valTargetFlatEq;\r
54 bool CloseAll;\r
55 bool longBias=false;\r
56 int totalOrders=0;\r
57 //double currentPL=0;\r
59 double startPrice=0.0;\r
61 double avgEntryPrice=0.0;\r
63 int    digits=4;\r
64 double pip = 0;\r
66 bool initOrdersDone=false;\r
68 int init()\r
69 {\r
70     digits=MarketInfo(Symbol(),MODE_DIGITS); \r
71  \r
72     if (digits < 4)\r
73         pip = 0.01;\r
74     else\r
75         pip = 0.0001;\r
76     \r
77     resetAccountCalc();\r
78    \r
79     return(0);\r
80 }\r
82 int deinit()\r
83 {\r
84     //---- TODO: add your code here\r
85     deleteAll();\r
86     //----\r
87     return(0);\r
88 }\r
89   \r
91 void deleteAll(){ \r
92  \r
93     for(int j = 0; j <= 10000; j++){\r
94         ObjectDelete("Comment" + j);     \r
95       \r
96     } \r
97      \r
98 }\r
101 int start()\r
103     //DollarProfitTarget = StrToDouble(DoubleToStr(AccountEquity()*ProfitPct/100.0,2));\r
104     if(startPrice ==0.0) {\r
105         startPrice=iOpen( NULL, PERIOD_M15, 0);\r
106         Print("----- startPrice ...... "+startPrice);\r
107     }\r
108         \r
109     if(!initOrdersDone && inTradingHours()){\r
110          \r
111           \r
112         placeBuyStopOrder();\r
113         placeSellStopOrder();\r
114         if(InitialBuyOrder) {\r
115             longBias=true;\r
116             placeInitBuyOrder();\r
117         }else{\r
118             placeInitSellOrder();\r
119             longBias=false;\r
120         }\r
121             \r
122         initOrdersDone=true;\r
123     }\r
124      \r
125     double currentPL = getEAPL();\r
126     bool panic_mode = false;\r
127      \r
128     //  if (!CloseAll && currentPL > DollarProfitTarget)\r
129         \r
130     if (currentPL+AccountBalance() < Panic_At) panic_mode = true;\r
132     if (!CloseAll && \r
133         (currentPL+AccountBalance() > valTargetFlatEq ||\r
134          (panic_mode == true && currentPL > DollarProfitTarget)\r
135          ))\r
136         {           \r
137             Print("================ closing ================");\r
138             Print("currentPL "+currentPL+" AccountBalance "+AccountBalance()+" valTargetFlatEq "+valTargetFlatEq);\r
139             CloseAllTrades();\r
140             return(0);\r
141         }\r
142      \r
143         \r
144     if(inTradingHours()) { \r
146         if (Paranoid_Stop_Loss) {\r
147             double servers_min_stop = (Slippage + MarketInfo(Symbol(), MODE_STOPLEVEL)) * Point;\r
148             if(iClose( NULL, PERIOD_M15, 0) > startPrice + servers_min_stop) {\r
149                 if(MoveSL2BE)   moveSellOrderToBE();\r
150             } else if(iClose( NULL, PERIOD_M15, 0) < startPrice - servers_min_stop) {\r
151                 if(MoveSL2BE) moveBuyOrderToBE();  \r
152             }\r
153         }\r
155         if(iClose( NULL, PERIOD_M15, 0) > (startPrice + PipStep*pip)){\r
156             longBias=true;\r
157             startPrice=iClose( NULL, PERIOD_M15, 0);\r
158             Print("----- moved to startPrice ...... "+startPrice);\r
159             // need to set the sl to BE of last order\r
160             if(MoveSL2BE)   moveSellOrderToBE();\r
161             CloseAllPendingOrders();\r
162             // place buy limit order\r
163             if (inBands()) {\r
164                 placeBuyStopOrder();  \r
165                 placeSellStopOrder();      \r
166             }\r
167         } else if(iClose( NULL, PERIOD_M15, 0) < (startPrice - PipStep*pip)){\r
168             longBias=false;\r
169             startPrice=iClose( NULL, PERIOD_M15, 0);\r
170             Print("------ moved to startPrice ...... "+startPrice);\r
171             // need to set the sl to BE of last order\r
172             if(MoveSL2BE) moveBuyOrderToBE();  \r
173             CloseAllPendingOrders();\r
174             // place buy limit order\r
175             if (inBands()) {\r
176                 placeSellStopOrder();\r
177                 placeBuyStopOrder();\r
178             }\r
179         }\r
181     }\r
182         \r
183     printComment(currentPL);\r
185     \r
186 bool inTradingHours(){\r
187     \r
188     bool returnVal = true;\r
190     if (UseTradingHours)\r
191         {\r
192             if (TimeHour(iTime( NULL, 0, 0) ) < StartHour &&\r
193                 ((TimeHour(iTime( NULL, 0, 0)) > FinishHour && FinishHour != 0) ||\r
194                  (TimeHour(iTime( NULL, 0, 0)) < 24 && FinishHour == 0)))\r
195                 {\r
196                     returnVal=false;\r
197                 }\r
198            \r
199         }\r
200     \r
201     return(returnVal);\r
204 bool inBands()\r
206     bool returnVal = true;\r
208     if (Use_Bollinger_Bands)\r
209         {\r
210             double upper_band = iBands(NULL, PERIOD_M15, Bollinger_Band_Days, Bollinger_Band_Deviation, 0, PRICE_CLOSE, MODE_UPPER, 0);\r
211             double lower_band = iBands(NULL, PERIOD_M15, Bollinger_Band_Days, Bollinger_Band_Deviation, 0, PRICE_CLOSE, MODE_LOWER, 0);\r
213             Print("upper_band: " + upper_band + " lower_band: " + lower_band);\r
214             if (upper_band - lower_band < Min_Bollinger_Bands_Width) returnVal = false;\r
215         }\r
217     return(returnVal);\r
218 }    \r
219     \r
220 void printComment(double currentPL){\r
221     \r
222         \r
223     double curPL = -valLastFlatEq + AccountBalance()+currentPL;\r
224     double drawdown = (curPL/valLastFlatEq)*100;\r
225      \r
226     string commentSt = "\nEquity "+DoubleToStr(valLastFlatEq,0) + " Dollar Profit "+DoubleToStr(DollarProfitTarget,2)+" PL " +DoubleToStr(curPL,2)\r
227         +" DD "+DoubleToStr(drawdown,2);\r
228         \r
229     ObjectCreate("CommentStr",OBJ_LABEL,0,0,0);\r
230     ObjectSet("CommentStr",OBJPROP_XDISTANCE,5);\r
231     ObjectSet("CommentStr",OBJPROP_YDISTANCE,40);\r
232     ObjectSet("CommentStr",OBJPROP_WIDTH,5);\r
233     ObjectSetText("CommentStr",commentSt,12,"Arial",DodgerBlue);\r
234          \r
235     string bias="Short";\r
236     double targetPrice=0;\r
237     if(longBias) {\r
238         bias="Long";\r
239         if(totalOrders>0) {\r
240            \r
241             //  Print("profit "+DollarProfitTarget+" totalOrders "+totalOrders+" LotSize "+LotSize+" qty "+MarketInfo(Symbol(), MODE_LOTSIZE) +" avgEntryPrice "+avgEntryPrice+\r
242             // " bottom "+totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE) +" top "+(DollarProfitTarget*pip));\r
243               \r
244             targetPrice = (DollarProfitTarget/(totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE)))+avgEntryPrice;\r
245         }\r
246     }else {\r
247         if(totalOrders>0) {\r
248             //  Print("totalOrders "+totalOrders+" LotSize "+LotSize+" qty "+MarketInfo(Symbol(), MODE_LOTSIZE) +" avgEntryPrice "+avgEntryPrice);\r
249               \r
250             targetPrice =  -(DollarProfitTarget/(totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE)))+avgEntryPrice;\r
251         }\r
252     }\r
253          \r
254     string commentSt1 = "\nBias "+bias + ", Orders "+totalOrders+", Target " +DoubleToStr(targetPrice,4) ;\r
255         \r
256     ObjectCreate("CommentStr1",OBJ_LABEL,0,0,0);\r
257     ObjectSet("CommentStr1",OBJPROP_XDISTANCE,5);\r
258     ObjectSet("CommentStr1",OBJPROP_YDISTANCE,60);\r
259     ObjectSet("CommentStr1",OBJPROP_WIDTH,5);\r
260     ObjectSetText("CommentStr1",commentSt1,12,"Arial",MediumSpringGreen);\r
261     \r
263     \r
264     \r
265 void resetAccountCalc(){\r
266     DollarProfitTarget = Profit_Fixed; \r
267     valLastFlatEq = AccountBalance(); \r
268     valTargetFlatEq =   valLastFlatEq +  DollarProfitTarget;\r
269     Print("\nDollarProfitTarget ...... "+DollarProfitTarget+" valLastFlatEq "+valLastFlatEq);\r
271     \r
272     \r
273 //+------------------------------------------------------------------+\r
274 //|                 Close all of the open trades.                    |\r
275 //+------------------------------------------------------------------+\r
277 void CloseAllTrades()\r
279  \r
280     for(int i = 0; i < OrdersTotal(); i++)\r
281         {\r
282             OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
284             if (OrderMagicNumber() == MagicNumber)\r
285                 {\r
286                     if (OrderType() == OP_BUY)\r
287                         {\r
288                             Comment("In full closure mode.  Closing a ", OrderSymbol(), " buy trade...");\r
289                             Print("======================================================== In full closure mode.  Closing a ", OrderSymbol(), " buy trade...");\r
290                             bool closeStatus = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),Slippage,Blue);\r
291                             if (!closeStatus) {\r
292                                 Print("Close Buy failed with error #",GetLastError());  \r
293                                 int check=GetLastError();\r
294                                 if(check!=0) Print("Close Buy failed with error: ",ErrorDescription(check));             \r
295                             }\r
296                             Sleep(100);\r
297                         }    \r
298                     else\r
299                         if (OrderType() == OP_SELL)\r
300                             {\r
301                                 Comment("In full closure mode.  Closing a ", OrderSymbol(), " sell trade...");\r
302                                 Print("============================================================In full closure mode.  Closing a ", OrderSymbol(), " sell trade...");\r
303                                 closeStatus = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),Slippage,Red);\r
304                                 if (!closeStatus) {\r
305                                     Print("Close Sell failed with error #",GetLastError());    \r
306                                     check=GetLastError();\r
307                                     if(check!=0) Print("Close Sell failed with error: ",ErrorDescription(check));           \r
308                                 }\r
309                                 Sleep(100);\r
310                             }else if (OrderType() == OP_BUYSTOP)\r
311                             {\r
312                                 Comment("In full closure mode.  Closing pending order ", OrderSymbol(), " buy trade...");\r
313                                 Print("======================================================== In full closure mode.  Closing a ", OrderSymbol(), " buy trade...");\r
314                                 closeStatus = OrderDelete(OrderTicket(),Blue);\r
315                                 if (!closeStatus) {\r
316                                     Print("Close Buy Stop failed with error #",GetLastError());       \r
317                                     check=GetLastError();\r
318                                     if(check!=0) Print("Close Buy Stop failed with error: ",ErrorDescription(check));         \r
319                                 }\r
320                                 Sleep(100);\r
321                             }    \r
322                         else\r
323                             if (OrderType() == OP_SELLSTOP)\r
324                                 {\r
325                                     Comment("In full closure mode.  Closing pending order ", OrderSymbol(), " sell trade...");\r
326                                     Print("============================================================In full closure mode.  Closing a ", OrderSymbol(), " sell trade...");\r
327                                     closeStatus=OrderDelete(OrderTicket(),Red);\r
328                                     if (!closeStatus) {\r
329                                         Print("Close Sell Stop with error #",GetLastError());   \r
330                                         check=GetLastError();\r
331                                         if(check!=0) Print("Close Sell Stop with error: ",ErrorDescription(check));             \r
332                                     }\r
333                                     Sleep(100);\r
334                                 }\r
335                 }\r
336         }\r
337     Sleep(10000);\r
338     resetAccountCalc();\r
339     startPrice=iOpen( NULL, PERIOD_M15, 0);\r
340     Print("resetting startPrice ...... "+startPrice);\r
341     initOrdersDone=false;\r
342   \r
346 void CloseAllPendingOrders()\r
348  \r
349     for(int i = 0; i < OrdersTotal(); i++)\r
350         {\r
351             OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
353             if (OrderMagicNumber() == MagicNumber)\r
354                 {\r
355                     // Print(" order type "+OrderType());\r
356                     if (OrderType() == OP_BUYSTOP)\r
357                         {\r
358                             Comment("..... Closing pending order ", OrderSymbol(), " buy trade...");\r
359                             Print("========================================================  Closing a ", OrderSymbol(), " buy stop order...");\r
360                             bool closeStatus = OrderDelete(OrderTicket(),Blue);\r
361                             if (!closeStatus) {\r
362                                 Print("Close Buy Stop failed with error #",GetLastError());       \r
363                                 int check=GetLastError();\r
364                                 if(check!=0) Print("Close Buy Stop failed with error: ",ErrorDescription(check));         \r
365                             }\r
366                             Sleep(100);\r
367                         }    \r
368                     else\r
369                         if (OrderType() == OP_SELLSTOP)\r
370                             {\r
371                                 Comment("..... Closing pending order ", OrderSymbol(), " sell trade...");\r
372                                 Print("=========================================================== Closing a ", OrderSymbol(), " sell stop order...");\r
373                                 closeStatus=OrderDelete(OrderTicket(),Red);\r
374                                 if (!closeStatus) {\r
375                                     Print("Close Sell Stop with error #",GetLastError());   \r
376                                     check=GetLastError();\r
377                                     if(check!=0) Print("Close Sell Stop with error: ",ErrorDescription(check));             \r
378                                 }\r
379                                 Sleep(100);\r
380                             }\r
381                 }\r
382         }\r
383      \r
386 double getEAPL()\r
388     double overallPL;\r
389     totalOrders=0;\r
390    \r
391     avgEntryPrice=0;\r
392     double totalEntryPrice=0.0;\r
393    \r
394    \r
395     for(int i = 0; i < OrdersTotal(); i++)\r
396         {\r
397             OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
398      \r
399             if (OrderMagicNumber() == MagicNumber && ( (OrderType() == OP_BUY)||(OrderType() == OP_SELL)))\r
400                 {      \r
401       \r
402                     double curPL = OrderProfit() + OrderSwap() + OrderCommission();\r
403                     double openPrice = OrderOpenPrice();\r
404                     //  Print(" openPrice "+openPrice);\r
405                     totalEntryPrice = totalEntryPrice + openPrice;\r
406                     //  Print(" totalEntryPrice "+totalEntryPrice);\r
407                     // Print("curPL "+curPL);\r
408                     overallPL = overallPL + curPL;\r
409                     totalOrders++;\r
410        \r
411                 }\r
412         }\r
413     \r
414     if(totalOrders>0) {\r
415         avgEntryPrice = totalEntryPrice/totalOrders;\r
416         //Print(" avgEntryPrice "+avgEntryPrice+" totalOrders "+totalOrders+" totalEntryPrice "+totalEntryPrice);\r
417     }\r
418    \r
419   \r
420     return(overallPL);\r
423         \r
424 void placeSellStopOrder(){\r
425      \r
426     double bid = MarketInfo(Symbol(), MODE_BID) ; \r
427           \r
428     double sellPrice = bid - (PipStep*pip);          \r
429     double sellSL = bid;\r
430           \r
431           \r
432     Print("Placing sell stop order at "+sellPrice+" with SL at "+sellSL);\r
433          \r
434     int  ticketshort = OrderSend(Symbol(),OP_SELLSTOP,LotSize,NormalizeDouble(sellPrice, digits),\r
435                                  Slippage,NormalizeDouble(sellSL, digits),0,"Short Speed Train",MagicNumber,0,Red);\r
436                                       \r
437     if (ticketshort < 0) {\r
438         //  Print("Short OrderSend OP_SELLSTOP failed with error #",GetLastError()); \r
439         int check=GetLastError();\r
440         if(check!=0) Print("Short OrderSend OP_SELLSTOP failed with error: ",ErrorDescription(check));    \r
441         return;\r
442     }\r
443           \r
444     Print("Sell stop order placed with ticket "+ticketshort+"  at "+sellPrice);\r
445           \r
446     shortOrderCount++;\r
447     //Sleep(1000);          \r
448             \r
449          \r
450      \r
452        \r
453 void placeBuyStopOrder(){\r
454      \r
455     double ask = MarketInfo(Symbol(), MODE_ASK) ; \r
456           \r
457     double buyPrice = ask + (PipStep*pip);          \r
458     double buySL = ask;\r
459           \r
460           \r
461     Print("Placing buy stop order at "+buyPrice+" with SL at "+buySL);\r
462          \r
463     int  ticketlong = OrderSend(Symbol(),OP_BUYSTOP,LotSize,NormalizeDouble(buyPrice, digits),\r
464                                 Slippage,NormalizeDouble(buySL, digits),0,"Long Speed Train",MagicNumber,0,Blue);\r
465                                       \r
466     if (ticketlong < 0) {\r
467         //Print("Long OrderSend OP_BUYSTOP failed with error #",GetLastError()); \r
468               \r
469         int check=GetLastError();\r
470         if(check!=0) Print("Long OrderSend OP_BUYSTOP failed with error: ",ErrorDescription(check));\r
472               \r
473         return;\r
474     }\r
475           \r
476     Print("Buy stop order placed with ticket "+ticketlong+"  at "+buyPrice);\r
477           \r
478     longOrderCount++;\r
479     // Sleep(1000);          \r
480             \r
481          \r
482      \r
484       \r
485       \r
486 void placeInitBuyOrder(){\r
487      \r
488     double ask = MarketInfo(Symbol(), MODE_ASK) ; \r
489           \r
490     double buySL = ask - (PipStep*pip);          \r
491          \r
492           \r
493           \r
494     Print("Placing buy  order at "+ask+" with SL at "+buySL);\r
495          \r
496     int  ticketlong = OrderSend(Symbol(),OP_BUY,LotSize,Ask,\r
497                                 Slippage,0,0,"Long Speed Train",MagicNumber,0,Blue);\r
498                                       \r
499     if (ticketlong < 0) {\r
500         // Print("Long OrderSend OP_BUY failed with error #",GetLastError()); \r
501         int check=GetLastError();\r
502         if(check!=0) Print("Long OrderSend OP_BUY failed with error: ",ErrorDescription(check));  \r
503         return;\r
504     }\r
505           \r
506     Print("Buy stop order placed with ticket "+ticketlong+"  at "+ask);\r
507           \r
508     int ticketlong1 = OrderModify(ticketlong,OrderOpenPrice(),NormalizeDouble(buySL, digits),0,0,Blue);\r
509     Sleep(1000);\r
510             \r
511     // Print("ticketlong1 "+ticketlong1);\r
512           \r
513     if (ticketlong1 < 1) {\r
514         // Print("Long OrderModify failed with error #",GetLastError()); \r
515         check=GetLastError();\r
516         // Print("check "+check);\r
517         if(check!=0) Print("Long OrderModify failed with error: ",ErrorDescription(check));\r
518     }\r
519             \r
520           \r
521     longOrderCount++;        \r
522      \r
524       \r
525       \r
526 void placeInitSellOrder(){\r
527      \r
528     double bid = MarketInfo(Symbol(), MODE_BID) ; \r
529           \r
530     double sellSL = bid + (PipStep*pip);            \r
531           \r
532     Print("Placing sell  order at "+bid+" with SL at "+sellSL);\r
533          \r
534     int  ticketshort = OrderSend(Symbol(),OP_SELL,LotSize,NormalizeDouble(bid, digits),\r
535                                  Slippage,0,0,"Short Speed Train",MagicNumber,0,Red);\r
536                                       \r
537     if (ticketshort < 0) {\r
538         //Print("Short OrderSend OP_SELL failed with error #",GetLastError()); \r
539         int check=GetLastError();\r
540         if(check!=0) Print("Short OrderSend OP_SELL failed with error: ",ErrorDescription(check));\r
541         return;\r
542     }\r
543           \r
544     Print("Sell stop order placed with ticket "+ticketshort+"  at "+bid);\r
545           \r
546     int ticketshort1 = OrderModify(ticketshort,OrderOpenPrice(),NormalizeDouble(sellSL, digits),0,0,Red);\r
547     Sleep(1000);\r
548           \r
549     if (ticketshort1 < 1) {\r
550         //  Print("Short OrderModify failed with error #",GetLastError()); \r
551         check=GetLastError();\r
552         if(check!=0) Print("Short OrderModify failed with error: ",ErrorDescription(check));\r
553     }\r
554           \r
555     shortOrderCount++;        \r
556      \r
558       \r
559       \r
560 void moveBuyOrderToBE(){\r
561     int lastTicket=0;\r
562       \r
563     for(int j = 0; j < OrdersTotal(); j++)\r
564         {\r
565             OrderSelect(j,SELECT_BY_POS,MODE_TRADES);\r
566             // Print("order type "+OrderType());\r
567             \r
568             if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUYSTOP)\r
569                 {\r
570                     int ticketId = OrderTicket() ;\r
571                     //Print("ticketId "+ticketId);\r
572                     if( ticketId>   lastTicket) lastTicket=ticketId;  \r
573                   \r
574                     //Print("lastTicket "+lastTicket);            \r
575                 }      \r
576         }\r
577        \r
578     if(lastTicket >0) {             \r
579         Print(" ----------------------- Moving long order number "+lastTicket+ " to BE at  "+OrderOpenPrice());    \r
580        \r
581         int ticketlong = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Blue);\r
582           \r
583                                       \r
584         if (ticketlong < 0) {\r
585             //  Print("Long SL2BE order failed with error #",GetLastError()); \r
586             int check=GetLastError();\r
587             if(check!=0) Print("Long SL2BE order failed with error: ",ErrorDescription(check));\r
588             return;\r
589         }\r
590               \r
591            \r
592     }           \r
593          \r
594      \r
596      \r
597      \r
598      \r
599          \r
600       \r
601       \r
602       \r
603 void moveSellOrderToBE(){\r
604     int lastTicket=0;\r
605       \r
606     for(int j = 0; j < OrdersTotal(); j++)\r
607         {\r
608             OrderSelect(j,SELECT_BY_POS,MODE_TRADES);\r
610             // Print("order type "+OrderType());\r
611             if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELLSTOP)\r
612                 {\r
613                     int ticketId = OrderTicket() ;\r
614                     // Print(" ticketId "+ticketId);    \r
615                     if( ticketId>   lastTicket) lastTicket=ticketId; \r
616                     // Print(" lastTicket "+lastTicket);             \r
617                 }      \r
618         }\r
619        \r
620           \r
621        \r
622        \r
623        \r
624     if(lastTicket>0) {\r
625        \r
626         Print("------------ Moving short order number "+lastTicket+ " to BE at  "+OrderOpenPrice());\r
627         int ticketshort = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Red);          \r
628                                       \r
629         if (ticketshort < 0) {\r
630             //Print("Short SL2BE order failed with error #",GetLastError()); \r
631             int check=GetLastError();\r
632             if(check!=0) Print("Short SL2BE order failed with error: ",ErrorDescription(check));\r
633             return;\r
634         }\r
635              \r
636     }\r
637           \r
638                         \r
639          \r
640      \r
642       \r
643      \r
644   \r
645    \r